public class MyQueue {
    private ListNode front;
    private ListNode rear;
    private int usedSize;
    static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }

    public void offer(int x) {
        ListNode listNode = new ListNode(x);
        if(front == null) {
            front = rear = listNode;
        }else {
            listNode.next = front;
            front.prev = listNode;
            front = listNode;
        }
        usedSize++;
    }
    public int poll() {
        if(front == null) {
            throw new NullPointerException();
        }
        int ret = rear.val;
        if(front == rear) {
            front = null;
            rear = null;
            usedSize--;
            return ret;
        }else {
            rear = rear.prev;
            rear.next = null;
            usedSize--;
            return ret;
        }
    }
    public int peek() {
        if(front == null) {
            throw new NullPointerException();
        }
        return front.val;
    }
    public int size() {
        return usedSize;
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }
}
